//
//  EditPsMessageController.m
//  JF_New_DYKT
//
//  Created by 陈雷 on 2017/9/2.
//  Copyright © 2017年 陈雷. All rights reserved.
//

#import "EditPsMessageController.h"

@interface EditPsMessageController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tbv;

@property (nonatomic,strong) UITextField *tf;

@end

@implementation EditPsMessageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([self.titleStr isEqualToString:@"姓名"]) {
        self.tf.placeholder = @"请输入您的姓名";
    }else if ([self.titleStr isEqualToString:@"职业"]){
        self.tf.placeholder = @"请输入您的职业";
    }else if ([self.titleStr isEqualToString:@"个人简介"]){
        self.tf.placeholder = @"请输入您的个人简介";
    }

    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 80, 40);
    rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor colorWithHexString:@"1cda9f"] forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightBtn addTarget:self action:@selector(completeAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    
    [self baseConfig];
}

- (void)baseConfig{
    
    self.title = [NSString stringWithFormat:@"修改%@",self.titleStr];
    
    self.tbv = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KSCreenWidth, KSCreenHeight - 64) style:UITableViewStyleGrouped];
    self.tbv.delegate = self;
    self.tbv.dataSource = self;
    [self.view addSubview:self.tbv];
    self.tbv.separatorColor = [UIColor colorWithHexString:@"eeeeee"];
    self.tbv.separatorInset = UIEdgeInsetsZero;
    self.tbv.backgroundColor = [UIColor colorWithHexString:@"f6f6f6"];
    
    self.tbv.tableFooterView = [UIView new];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

        [cell addSubview:self.tf];
        
    }
    return cell;
}


- (void)completeAction{

    [self.tf resignFirstResponder];
    
    if (self.tf.text.length <= 0) {
        
        [JKToast showWithText:@"内容不能为空"];
        
        return;
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@user/Personal/saveBaseInfo",kDomainName];
    
    NSDictionary *paramDic = nil;
    if ([self.titleStr isEqualToString:@"姓名"]) {
        paramDic = @{@"key":@"nick_name",
                     @"world":self.tf.text,
                     @"userId":USERID
                     };
    }else if ([self.titleStr isEqualToString:@"职业"]){
        paramDic = @{@"key":@"occupation",
                     @"world":self.tf.text,
                     @"userId":USERID
                     };
    }else if ([self.titleStr isEqualToString:@"个人简介"]){
        paramDic = @{@"key":@"user_sign",
                     @"world":self.tf.text,
                     @"userId":USERID
                     };
    }
    
    
    BaseRequest *request =  [BaseRequest sharedBaseRequest];
    [request post:urlStr params:paramDic success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"code"] isEqualToString:@"1"] ) {
            
            [JKToast showWithText:responseObject[@"message"]];
            self.transB(self.tf.text);
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [JKToast showWithText:responseObject[@"message"]];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];


}


- (UITextField *)tf{
    if (!_tf) {
        _tf = [[UITextField alloc]initWithFrame:CGRectMake(10, 0, KSCreenWidth - 20, 50)];
        _tf.clearButtonMode = UITextFieldViewModeAlways;
        _tf.text = self.defaultStr;
        _tf.tag = 10088;
    }
    return _tf;
}


@end
